package com.opensource.component.dinging.message;

import com.opensource.component.dinging.message.enums.MessageType;

import java.util.HashMap;
import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.util.StringUtils;

/**
 * created by zhanglong and since  2019/12/27  10:03 下午
 *
 * @description: 描述
 */
@Data
public class MarkdownMessage extends BaseDingMessage {

    private Markdown markdown;

    private AtPhone at = new AtPhone();
    /**
     * 没有@功能，如果操作请 AtPhone
     */
    public MarkdownMessage(Markdown markdown) {
        this.markdown = markdown;
    }
    /**
     * 传入at进行@
     */
    public MarkdownMessage(Markdown markdown, AtPhone at) {
        this.markdown = markdown;
        this.at = at == null ? new AtPhone() : at;
    }

    @Override
    public Map<String, Object> toMessageMap() {
        HashMap<String, Object> resultMap = new HashMap<>(8);
        resultMap.put("msgtype", MessageType.MARKDOWN.value());
        HashMap<String, String> markdownItems = new HashMap<>(8);
        markdownItems.put("title", markdown.getTitle());
        markdownItems.put("text", markdown.getText());
        resultMap.put("markdown", markdownItems);
        HashMap<String, Object> atItems = new HashMap<>(8);
        atItems.put("atMobiles", at.getAtMobiles());
        atItems.put("isAtAll", at.isAtAll());
        resultMap.put("at", atItems);
        return resultMap;
    }

    @Data
    public static class Markdown {
        /**
         * 消息标题
         */
        private String title;
        /**
         * 消息简介
         */
        private String text;

        /**
         *
         * @param title 标题
         * @param context 内容
         */
        public Markdown(String title, String context) {
            if (StringUtils.isEmpty(title) || StringUtils.isEmpty(context)) {
                throw new IllegalArgumentException("please check the necessary parameters!");
            }
            this.title = title;
            this.text = context;
        }
    }
}
